package org.web.ssm.service.wechatService.wechatServiceImpl;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import org.web.ssm.model.wechatModel.TulingMessage;
import org.web.ssm.model.wechatModel.TulingRequest;
import org.web.ssm.service.wechatService.ITulingService;
import org.web.ssm.utils.HttpClientUtils;

import java.io.IOException;

/**
 * Created by LUOLIANG on 2016/6/18.
 */
@Service
public class TulingServiceImpl implements ITulingService {
    private static final String URL = "http://www.tuling123.com/openapi/api";
    private static final String APIKEY = "f1c79165717f702cfd689532f56b323d";

    @Override
    public String getTextMessage(TulingRequest tulingRequest) {
        try {
            tulingRequest.setKey(APIKEY);
            System.out.println(tulingRequest);
            String textmessage = HttpClientUtils.doPostStr(URL, JSON.toJSONString(tulingRequest));
            TulingMessage tulingMessage = JSON.parseObject(textmessage, TulingMessage.class);
            System.out.println(tulingMessage);
            StringBuffer sb = new StringBuffer();
            sb.append("胡逼说：\n" + tulingMessage.getText()).toString();
            if (tulingMessage.getUrl() != "" && tulingMessage.getUrl() != null) {
                sb.append("\n<a href=" + "\"" + tulingMessage.getUrl() + "\"" + ">传送门</a>");
            }
            return sb.toString();
        } catch (IOException e) {
            return new StringBuffer().append("出现异常").toString();
        }
    }
}
